import random
import threading
from queue import Queue
from time import sleep


class Producer:
    def __init__(self, queue: q, name):
        self.queue = queue
        self.name = name

    def produce(self, item) -> None:
        print(f'{self.name}生产对象{item=}')
        if self.queue.full():
            print("队列满了")
        else:
            self.queue.put(item)


class Consumer:
    def __init__(self, queue, name):
        self.queue = queue
        self.name = name

    def consume(self):
        while True:
            if self.queue.empty():
                print("-------no item------")
            try:
                item = self.queue.get_nowait(timeout=1)
                if not item:
                    print(f'queue is empty')
                else:
                    print(f"{self.name}消费对象{item=}")
            except Exception as e:
                print(e)

if __name__ == '__main__':
    q = Queue(maxsize=1024)
    producer_number = 2
    consumer_number = 3
    for i in range(consumer_number):
        consumer = Consumer(q, f'第{i}个消费者')
        threading.Thread(target=consumer.consume).start()
    # for i in range(producer_number - 1):
    #     producer = Producer(q, f'第{i}个生产者')
    #     threading.Thread(target=, args=(random.randint)).start()

    producer = Producer(q, f'第{producer_number - 1}个生产者')
    for i in range(200):
        producer.produce(random.randint)
        sleep(1)
